home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 1.1 KB | 56 lines |
- import java.awt.*;
- import java.applet.*;
-
- public class ClickApplet extends Applet {
-
- boolean clicked;
- int counter;
-
- public void init() {
- add(new ClickCanvas(this));
- add(new ClickCanvas(this));
- }
-
- public boolean mouseDown(Event e, int x, int y) {
- synchronized (this) {
- clicked = true;
- notify();
- }
- counter++;
-
- Thread.currentThread().yield();
- clicked = false;
- return super.mouseDown(e, x, y);
- }
-
- }
-
- class ClickCanvas extends Canvas implements Runnable {
- ClickApplet applet;
-
- ClickCanvas(ClickApplet applet) {
- this.applet = applet;
- resize(30, 30);
- new Thread(this).start();
- }
-
- public void run() {
- while (true) {
- synchronized (applet) {
- while (!applet.clicked) {
- try {
- applet.wait();
- } catch (InterruptedException x) {
- }
- }
- }
- repaint();
- }
- }
-
- public void paint(Graphics g) {
- g.drawString(new Integer(applet.counter).toString(), 10, 20);
- }
-
- }
-